package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.ColdCleanRoomRecordReq;
import com.ruibang.glass.produce.entity.DefectDirectly;
import com.ruibang.glass.produce.entity.ProduceBcpCleanRoomRecord;
import com.ruibang.glass.produce.entity.ProduceColdCleanRoomRecord;
import com.ruibang.glass.produce.mapper.ProduceColdCleanRoomRecordMapper;
import com.ruibang.glass.produce.service.ProduceColdCleanRoomRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 生产-洁净室-冷端洁净室环境记录 服务实现类
 * </p>
 *
 * @author 宋锦康
 * @since 2024-03-11
 */
@Slf4j
@Service
public class ProduceColdCleanRoomRecordServiceImpl extends ServiceImpl<ProduceColdCleanRoomRecordMapper, ProduceColdCleanRoomRecord> implements ProduceColdCleanRoomRecordService {

    @Override
    public IPage<ProduceColdCleanRoomRecord> pageQuery(ColdCleanRoomRecordReq coldCleanRoomRecordReq) {
        QueryWrapper<ProduceColdCleanRoomRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(ProduceColdCleanRoomRecord::getIsDelete, Constants.ZERO_STR);
        if (coldCleanRoomRecordReq.getStartTime() != null && coldCleanRoomRecordReq.getEndTime() != null) {
            qw.lambda().between(ProduceColdCleanRoomRecord::getDate, coldCleanRoomRecordReq.getStartTime(), coldCleanRoomRecordReq.getEndTime());
        }
        return this.page(new Page<>(coldCleanRoomRecordReq.getPageNo(), coldCleanRoomRecordReq.getPageSize()), qw.lambda());
    }

    @Override
    public void delete(String id) {
        lambdaUpdate().eq(ProduceColdCleanRoomRecord::getId, id).set(ProduceColdCleanRoomRecord::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public void export(ColdCleanRoomRecordReq coldCleanRoomRecordReq, HttpServletResponse response) {
        coldCleanRoomRecordReq.setPageNo(-1);
        coldCleanRoomRecordReq.setPageSize(-1);
        IPage<ProduceColdCleanRoomRecord> page = this.pageQuery(coldCleanRoomRecordReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<ProduceColdCleanRoomRecord> produceColdCleanRoomRecordList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), ProduceColdCleanRoomRecord.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(produceColdCleanRoomRecordList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }
}
